{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Iteration and Stopping\n",
    "\n",
    "Consider the same road as in the last demonstration. Now that we know how to read information from an array that represents the road, we want to act on this information! It will be up to you to tell the car to stop if it's right in front of the stop sign.\n",
    "\n",
    "<img src=\"files/images/stop_4.png\" width=\"50%\" height=\"50%\" >\n",
    "\n",
    "### The road"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<type 'numpy.ndarray'>\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# The 1x7 road\n",
    "road = np.array(['r', 'r', 'r', 'r', 'r', 's', 'r'])\n",
    "x = type(road)\n",
    "\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### TODO: Complete  `find_stop_index`\n",
    "\n",
    "Complete this function so that it returns the index of the cell that is *right before* the stop sign (ex. 0, if the stop sign is at index 1)!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# This function takes in the road and determines where to stop\n",
    "def find_stop_index(road):\n",
    "    \n",
    "    ## TODO: Iterate through the road array\n",
    "    ## TODO: Check if a stop sign ('s') is found in the array\n",
    "    ## If one is, break out of your iteration\n",
    "    ## and return the value of the index that is *right before* the stop sign\n",
    "    ## Change the stop_index value\n",
    "    stop = -1\n",
    "    for sign in road:\n",
    "        if sign =='s':\n",
    "            stop = road.find('s')\n",
    "            break\n",
    "    return stop\n",
    "    \n",
    "    \n",
    "    '''length = len(road)\n",
    "    for index in range(0, length):\n",
    "    # Find and store the value at each index\n",
    "    value = road[index]'''\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Testing Cell\n",
    "\n",
    "Run this cell and it will compare the output of your function with the correct, expected output.\n",
    "\n",
    "**Assertions**\n",
    "\n",
    "This cell is using something called `assertions` in Python, which are statements that check the validity of code. In this case, the first assertion checks that the output of your function: `stop`, is equal to the expected output: `correct_stop` and then prints out feedback!\n",
    "\n",
    "**Your code should pass both tests and work for any 1D road.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'numpy.ndarray' object has no attribute 'find'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-3-169aabd97488>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[1;31m# This line calls the stop function and stores the output\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0mstop\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m-\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m \u001b[0mstop\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mfind_stop_index\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mroad\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      7\u001b[0m \u001b[0mcorrect_stop\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0marray_solution\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mstop_test\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mroad\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      8\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m<ipython-input-2-3664ccfcd30b>\u001b[0m in \u001b[0;36mfind_stop_index\u001b[1;34m(road)\u001b[0m\n\u001b[0;32m     10\u001b[0m     \u001b[1;32mfor\u001b[0m \u001b[0msign\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mroad\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     11\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0msign\u001b[0m \u001b[1;33m==\u001b[0m\u001b[1;34m's'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 12\u001b[1;33m             \u001b[0mstop\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mroad\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfind\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m's'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     13\u001b[0m             \u001b[1;32mbreak\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     14\u001b[0m     \u001b[1;32mreturn\u001b[0m \u001b[0mstop\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mAttributeError\u001b[0m: 'numpy.ndarray' object has no attribute 'find'"
     ]
    }
   ],
   "source": [
    "# Test code - do not change\n",
    "import array_solution\n",
    "# stop = 0\n",
    "# This line calls the stop function and stores the output\n",
    "stop = -1\n",
    "stop = find_stop_index(road)\n",
    "correct_stop = array_solution.stop_test(road)\n",
    "\n",
    "# Assertion, comparison test\n",
    "try:\n",
    "    assert(stop == correct_stop)\n",
    "    print('That\\'s right!')\n",
    "except:\n",
    "    print ('Your code returned that the stop index is: ' +str(stop) \n",
    "           + ', which does not match the correct value: ' +str(correct_stop))\n",
    "\n",
    "# Test 2\n",
    "stop2 = find_stop_index(array_solution.test_road)\n",
    "correct_stop2 = array_solution.stop_test(array_solution.test_road)\n",
    "\n",
    "try:\n",
    "    assert(stop2 == correct_stop2)    \n",
    "    print('You passed the second test!')\n",
    "except:\n",
    "    print ('For test 2, your code returned that the stop index is: ' +str(stop2) \n",
    "           + ', which does not match the correct value.')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
